<template>
  <div class="login">
    <div class="title">物业管理系统</div>
    <el-form
      ref="form"
      :model="login"
      :rules="rules"
      class="login_container"
      label-width="70px"
      status-icon
      style="min-width: 420px; padding: 40px"
    >
      <div style="padding-top: 20px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="login.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="login.password"
            autocomplete="off"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <div style="display: flex">
            <el-input
              v-model="login.captcha"
              style="flex: 1; margin-right: 10px"
            ></el-input>
            <Random
              :identifyCode="identifyCode"
              @click.native="getCode"
            ></Random>
          </div>
        </el-form-item>
      </div>

      <div style="">
        <el-form-item label=" ">
          <el-button type="primary" @click="submit" style="width: 100%"
            >登录</el-button
          >
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCode, login } from "@/api/login";
import Random from "@/components/Random.vue";

export default {
  components: { Random },
  data() {
    return {
      login: {
        username: "chenbozhi",
        password: "12345678",
        captcha: "",
      },
      // 校验规则
      rules: {
        username: [
          {
            required: "true",
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: "true",
            message: "请输入密码",
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: "true",
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
      identifyCode: "",
    };
  },
  methods: {
    submit() {
      // 表单的校验
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const userInfo = {
            ...this.login,
            permission: false,
          };
          const data = await login({ ...this.login });
          window.localStorage.setItem("token", data);
          this.$store.commit("setInfo", userInfo);
          window.localStorage.setItem("userInfo", JSON.stringify(userInfo));
          this.$router.push("/home");
        }
      });
    },
    async getCode() {
      const data = await getCode();
      this.identifyCode = data;
    },
  },
  mounted() {
    this.getCode();
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: url(../assets/images/bg6.png);
  background-size: 100vw 100vh;

  .title {
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    color: #1434a4;
    font-family: fantasy;
  }
}

.login_container {
  overflow: auto;
  border: 1px solid #eaeaea;
  // margin: auto;
  // 居中
  padding: 10px 35px 10px 35px;
  box-sizing: border-box;
  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 25px #cac6c6;

  .login_title {
    color: #505458;
    // 左右居中
    text-align: center;
    margin-bottom: 40px;
    min-width: 400px;
  }

  .el-input {
    width: 100%;
  }
}

.des {
  background: white;
  padding: 40px;
  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 25px #cac6c6;
}
</style>
